package tms.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Aircraft {
    public static final String TABLE_NAME = "Aircraft";
    public static String[] COLUMNS = {"Name", "Code", "IsActive"};

    private int id = -1;
    private String name = "";
    private String code = "";
    private boolean isActive = false;

    public String[] getValues() {
        return new String[] {"'" + getName() + "'", "'" + getCode() + "'",
                isActive() == true ? "1" : "0"};
   }

    static Aircraft createEntityFrom(Map<String, Object> map) {

        Aircraft entity = new Aircraft();

        entity.setId((Integer) map.get("Id".toLowerCase()));
        entity.setName((String) map.get("Name".toLowerCase()));
        entity.setCode((String) map.get("Code".toLowerCase()));
        entity.setActive((Integer) map.get("IsActive".toLowerCase()) == 1);

        return entity;
   }

    public String toString(){
        return name + " (" + code + ")";
   }

    @XmlElement
    public int getId() {
        return id;
   }
    public void setId(int id) {
        this.id = id;
   }
    
    @XmlElement
    public String getName() {
        return name;
   }
    public void setName(String name) {
        this.name = name;
   }
    
    @XmlElement
    public String getCode() {
        return code;
   }
    public void setCode(String code) {
        this.code = code;
   }

    @XmlElement
    public boolean isActive() {
        return isActive;
   }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
   }
    
    
    public static Aircraft getFirstDynamic(String where) {
        Map<String, Object> map = DbManager.getFirstDynamic(Aircraft.TABLE_NAME, where);
        return map != null ? Aircraft.createEntityFrom(map) : null;
    }

    /**
     * Get a list of Aircraft corresponding to where clause.
     * @param where condition (ex: Username = 'Tony')
     * @return corresponding list of Aircraft or an empty list
     */
    public static List<Aircraft> getListDynamic(String where) {
        List<Aircraft> lsResult = new ArrayList<Aircraft>();

        List<Map<String, Object>> rs = DbManager.getListDynamic(Aircraft.TABLE_NAME, where);

        for (Map<String, Object> map : rs) {
            lsResult.add(Aircraft.createEntityFrom(map));
        }

        return lsResult;
    }
}
